package demo07String;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 09 - 27
 * Time:16:54
 */
public class Test {
    public static int countSegements2(String s){
        //(1）先对字符串去掉空格，使用trim()方法
        if(s == null && s.isEmpty()){
            return -1;
        }

        int ret = 1;

        return  ret;

    }
    /***
     * 5.统计字符串中的单词数
     * 统计字符串中的单词(单词指的是连续的的不是空格的字符)个数，
     * Hello,my name is john  5
    */
    public static int countSegements(String str){
        //(1）先对字符串去掉空格，使用trim()方法
        if(str == null && str.isEmpty()){
            return -1;
        }
        int ret = 0;
        //使用布尔变量start，在单词字符和空格的切换处累计单词的数量，并跳过多余的空格
        boolean start = false;
        //去除字符串前后两端的空格
        for (int i = 0; i < str.length(); i++) {
            if(start == true &&str.charAt(i) != ' '){
                start = true;
            }else if(start == false && str.charAt(i) == ' '){
                ret++;
                start = false;
            }
        }
        if(start == false){//处理最后一个单词
            ret++;
        }
        return  ret;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        System.out.println(countSegements(str));
    }
    /***
     * 4.不借用任何字符串库函数实现无冗余的接收两个字符串，然会把它们无冗余的连接起来
     * abc   def   abcdef
     * @param args
     */
    public static void main4(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        System.out.println(str1 + str2);
    }
    public static void main41(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();// abc def 将其看作是一行输入
        String[] str2 = str1.split(" ");
        String ret = "";
        for (int i = 0; i < str2.length; i++) {
            ret += str2[i];
        }
        System.out.println(ret);
    }
    /***
     * 3.翻转字符串
     * 给定一个转字符类型的数组chars一个整数size,
     * 请把左边大小为size的左半区去整体右移到右半区，右半区整体移动到左半区
     * 3 abcdefg  defgabc
     * k = 3，先对0 - k 下标对应字符进行反转
     *        再对 k —— len - 1下标进行反转
     *        再对 0 —— len - 1下标进行反转【整体进行翻转】
     */
    //
    public static void main3(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        String str = scanner.next();
        String ret = reverseK(str,k);
        System.out.println(ret);
    }

    private static String reverseK(String str, int k) {
        if(str == null || k <= 0 || k > str.length()){
            return null;
        }
        //分段进行逆置
        str = reverse(str,0,k - 1);
        str = reverse(str,k ,str.length() - 1);
        str = reverse(str,0 ,str.length() - 1);
        return str;

    }

    public static String reverse(String str,int left,int right){
        //(1）将字符串转换为数组
        char[] chars = str.toCharArray();
        //（2）交换
        while(left < right){
            char tmp = chars[left];
            chars[left] = chars[right];
            chars[right] = tmp;
            left++;
            right--;
        }
        //(3)将数组转换为字符串
        return  new String(chars);
    }
    /***
     * 2.字符串的逆置
     */
    public static String reverse(String str){
        //(1）将字符串转换为数组
        char[] chars = str.toCharArray();
        //（2）交换
        int left = 0;
        int right = chars.length - 1;
        while(left < right){
            char tmp = chars[left];
            chars[left] = chars[right];
            chars[right] = tmp;
            left++;
            right--;
        }
        //(3)将数组转换为字符串
        return  new String(chars);
    }
    /***
     * 1.检查两个字符串是否相等
     * 给定两个字符串数组word1 和word2 ，如果两个数组表示的字符串相等，则返回true；否则返回false
     * 数组表示的字符串：是由数组中的所有元素按顺序连接形成的字符串
     * word1 = ["ab","c"]  word2 = ["a","bc"]   —— true
     * @param word1
     * @param word2
     * @return
     */
    public static boolean arrayStringAreEqual(String[] word1,String[] word2){
        String str1 = "";
        String str2 = "";
        for (int i = 0; i < word1.length; i++) {
            str1 += word1[i];
        }
        for (int i = 0; i < word2.length; i++) {
            str2 += word2[i];
        }
        return str1.equals(str2);
    }

    public static void main1(String[] args) {
        String[] word1 = {"a","bc"};
        String[] word2 = {"a","bc"};
        System.out.println(arrayStringAreEqual(word1, word2));


    }
}
